<?php


namespace App\backend\services;


use App\backend\models\GoodParamsModel;

class GoodParamServices
{

    /**
     * 获取 - 商品参数列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $where_data = [];
        if (isset($data['name']) && !empty($data['name'])) {
            $where_data[] = ['name','like',"%{$data['name']}%"];
        }

        $list = GoodParamsModel::listed($page, $limit, $where_data);
        if(empty($list)) {
            $list = [];
        } else {
            foreach ($list as $v_data) {
                if (is_array($v_data)) {
                    foreach ($v_data as $value) {
                        $value->params = json_decode($value->params, true);
                    }
                }
            }
        }
        return $list;
    }

    /**
     * 添加/编辑 - 商品参数
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodSkuModify($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $ids['id'] = !empty($data['params_id']) ? $data['params_id'] : 0;unset($data['params_id']);

        if (isset($ids['id']) && !empty($ids['id'])) {
            $data['updated_at'] = time();
            $list = GoodParamsModel::updatedData($ids, $data);
        } else {
            $data['created_at'] = time();
            $list = GoodParamsModel::insertData($data);
        }

        return $list;
    }

    /**
     * 删除 - 商品参数
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodParamsDelete($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = isset($data['params_id']) && !empty($data['params_id']) ? $data['params_id'] : 0;unset($data['params_id']);
        $list = GoodParamsModel::updatedData($data, ['status' => 1]);
        return $list;
    }

    /**
     * 修改 - 商品参数
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodParamsState($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = isset($data['params_id']) && !empty($data['params_id']) ? $data['params_id'] : 0;unset($data['params_id']);
        $update_data['state'] = isset($data['state']) && !empty($data['state']) ? $data['state'] : 0;unset($data['state']);
        $list = GoodParamsModel::updatedData($data, $update_data);
        return $list;
    }

    /**
     * 详情 - 商品参数
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodParamsInfo($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $select_data = ['id', 'name', 'type', 'params', 'item_num', 'state', 'created_at'];
        $data['id'] = isset($data['params_id']) && !empty($data['params_id']) ? $data['params_id'] : 0;unset($data['params_id']);
        $list = GoodParamsModel::info($data, $select_data);
        if(empty($list)) {
            $list = [];
        } else {
            $list->params = json_decode($list->params, true);
        }
        return $list;
    }

}
